<?php

namespace app\admin\controller;

use app\common\model\Fight as FightModel;
use app\common\model\Game as GameModel;
use app\common\model\Match as MatchModel;
use app\common\model\GameJoin;
use app\common\model\MatchJoin;

class Fight extends Base
{
    /**
     * 列表
     * @return \think\response\Json
     */
    public function game_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $data['status'] = input('status', 'all');
            $where = [];
            if ($data['name']) {
                $where['name'] = ['like', "%{$data['name']}%"];
            }
            if ($data['status'] != 'all') {
                $where['status'] = $data['status'];
            }
            $list = GameModel::getPage($where, $page, $size, 'start_time desc');
            if ($list) {
                foreach ($list as $key => $value) {
                    if ($value['start_time'] > date("Y-m-d H:i:s")) {
                        $list[$key]['game_status'] = '未举办';
                    } else if (date('Y-m-d H:i:s') > $value['end_time']) {
                        $list[$key]['game_status'] = '已举办';
                    } else {
                        $list[$key]['game_status'] = '举办中';
                    }
                }
            }
            $total = GameModel::getCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    public function team_list()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['gid']) {
                $fids = GameJoin::getColumn(['gid' => $data['gid']], 'fid', 'id asc');
                $where = [];
                if ($fids) {
                    $where['id'] = ['in', $fids];
                    $list = FightModel::getAll($where);
                } else {
                    $list = [];
                }
                return returnJson(200, '', $list);

            }
            return returnJson(202, '参数错误');
        }
    }


    /**
     * @return \think\response\Json
     */
    public function del_fight()
    {
        $input = input();
        if (!$input['id']) {
            return returnJson(1, 'error');
        }
        \app\common\model\Game::destroy(['id' => $input['id']]);
        return returnJson(0, '删除成功');
    }
    /**
     * @return \think\response\Json
     */
    public function del_match()
    {
        $input = input();
        if (!$input['id']) {
            return returnJson(1, 'error');
        }
        \app\common\model\Match::destroy(['id' => $input['id']]);
        MatchJoin::setDel(['mid'=>$input['id']]);
        return returnJson(0, '删除成功');
    }

    /**
     * @return array
     */
    public function shenhe()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $where = ['id' => $data['id']];
                $save_data = [
                    'status' => $data['status']
                ];
                $res = GameModel::update($save_data, $where);
                if ($res) {
                    return returnJson(200, '审核成功');
                } else {
                    return returnJson(202, '审核失败');
                }
            }
        }
    }

    /**
     * 赛事编辑
     * @return \think\response\Json
     */
    public function match_modify()
    {
        if (request()->isPost()) {
            $post = input('');
            $save_data = array(
                'name' => $post['name'],
                'start_time' => $post['time'][0],
                'end_time' => $post['time'][1],
                'content' => $post['content'],
                'update_time' => time(),
            );
            if ($post['id']) {
                $where = ['id' => $post['id']];
                $res = MatchModel::update($save_data, $where);
                if ($res) {
                    return returnJson(200, '编辑成功');
                } else {
                    return returnJson(202, '编辑失败');
                }
            } else {
                $save_data['create_time'] = time();
                $res = MatchModel::setInsertGetId($save_data);
                if ($res) {
                    return returnJson(200, '添加成功');
                } else {
                    return returnJson(202, '添加失败');
                }
            }
        }
    }


    /**
     * 大型赛事
     * @return \think\response\Json
     */
    public function match()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['name']) {
                $where['name'] = ['like', "%{$data['name']}%"];
            }
            $list = MatchModel::getPage($where, $page, $size, 'start_time desc');
            if ($list) {
                foreach ($list as $key => $value) {
                    if ($value['start_time'] > date("Y-m-d H:i:s")) {
                        $list[$key]['game_status'] = '未举办';
                    } else if (date('Y-m-d H:i:s') > $value['end_time']) {
                        $list[$key]['game_status'] = '已举办';
                    } else {
                        $list[$key]['game_status'] = '举办中';
                    }
                }
            }
            $total = MatchModel::getCount($where);
            return returnJson(200, '', compact('page', 'size', 'list', 'total'));
        }
    }

    /**
     * 大型赛事参赛人员
     * @return \think\response\Json
     */
    public function join_list()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['mid']) {
                $where['mid'] = $data['mid'];
                $list = MatchJoin::getAll($where, 'id desc');
                if (!$list) {
                    $list = [];
                }
                return returnJson(200, '', $list);
            }
            return returnJson(202, '参数错误');
        }
    }

}

